import { message } from 'antd';
import { reqSample, sortSample, bindSampleid, deleteSample, updataFibsem, updataQemscan } from '@/service/sample';

const Model = {
    namespace: "sample",
    state: {
        sample: [],
        edit: {},
        updataId: '',
        total: 0
    },
    effects: {
        *getSample({ payload }, { put, call }) {
            const res = yield call(reqSample, payload)
            if (res.code === 200) {
                yield put({
                    type: "saveSample",
                    payload: res.raw
                })
                yield put({
                    type: 'saveSampleTotal',
                    payload: res.total
                })
            } else {
                message.error("请求样品错误")
            }
        },
        *classifier({ payload }, { put, call }) {
            const res = yield call(sortSample, payload)
            if (res.code === 200) {
                yield put({
                    type: "saveSample",
                    payload: res.sample
                })
            }
        },
        *bindSampleid({ payload }, { put, call, select }) {
            const data = yield select(state => state.sample.samplemaps)
            if (data.length <= 5) {
                message.warning('未上传文件')
            }
            if (payload.sort == 0) {
                payload.sort = 1
            }
            if (payload.sort == 1) {
                data.mapsSize = payload.radio
                const res = yield call(bindSampleid, data)
                if (res.code === 200) {
                    message.success('样品与文件绑定成功')
                    yield put({
                        type: 'saveSamplemaps',
                        payload: ''
                    })
                } else {
                    message.error('样品与文件绑定失败')
                }
            }
            if (payload.sort == 2) {
                console.log('这里是FIB_SEM')
                data.fibsSize = payload.radio
                const res = yield call(updataFibsem, data)
                if (res.code === 200) {
                    message.success('样品与文件绑定成功')
                    yield put({
                        type: 'saveSamplemaps',
                        payload: ''
                    })
                } else {
                    message.error('样品与文件绑定失败')
                }
            }
            if (payload.sort == 3) {
                console.log('这里是QEMSCAN')
                data.mapsSize = payload.radio
                data.picRadio = payload.picRadio
                const res = yield call(updataQemscan, data)
                if (res.code === 200) {
                    message.success('样品与文件绑定成功')
                    yield put({
                        type: 'saveSamplemaps',
                        payload: ''
                    })
                } else {
                    message.error('样品与文件绑定失败')
                }
            }

        },
        *remainSamplemaps({ payload }, { put }) {
            console.log("保留的数据", payload)
            yield put({
                type: "saveSamplemaps",
                payload: payload
            })
        },
        *deleteSample({ payload }, { call, put }) {
            const res = yield call(deleteSample, payload)
            const data = yield call(reqSample, payload)
            if (res.code === 200) {
                message.success('删除成功 ')

                if (data.code == 200) {
                    message.success('成功')
                }
            } else {
                message.error('删除失败')
            }
        }
    },
    reducers: {
        saveSample(state, { payload }) {
            return { ...state, sample: payload }
        },
        saveSampleTotal(state, { payload }) {
            return { ...state, total: payload }
        },
        saveUpdataId(state, { payload }) {
            return { ...state, updataId: payload }
        },
        saveSamplemaps(state, { payload }) {
            return { ...state, samplemaps: payload }
        }
        // changeEdit(state,{payload}){
        //     return{...state,edit:{
        //         wellBlock: state.sample[payload].wellBlock,
        //         wellDeep: state.sample[payload].wellDeep,
        //         wellLayer: state.sample[payload].wellLayer,
        //         wellNum: state.sample[payload].wellNum
        //     }}
        // }
    }
}
export default Model